<?php


class Core_Model_Table_Setting extends Nuo_Db_Table
{

    protected $_name = 'core_settings';

    /**
     * get settings
     */
    public function getSetting($name, $default = null, $site_id = null)
    {
        if (null == $site_id)
        {
            $site_id = Nuo::getSiteId();
        }

        $row = $this -> fetchRow($this -> select() -> where('site_id=?', (int)$site_id) -> where('name=?', (string)$name));

        if (is_object($row))
        {
            return $row -> value;
        }
        return $default;

    }

    /**
     * set settings
     */
    public function setSetting($name, $value, $site_id = null)
    {
        if (null == $site_id)
        {
            $site_id = Nuo::getSiteId();
        }

        $row = $this -> fetchRow($this -> select() -> where('site_id=?', (int)$site_id) -> where('name=?', (string)$name));

        if (!is_object($row))
        {
            $row = $this -> fetchNew();
            $row -> setFromArray(array(
                'site_id' => $site_id,
                'name' => $name
            ));
        }

        $row -> value = $value;
        $row -> save();
    }

}
